﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using FineUICore;
using Kalman;
using Kalman.Extensions;
using Newtonsoft.Json.Linq;
using QuDao;

namespace AdminUI.Pages.STATS
{
    /// <summary>
    /// 每日销售统计
    /// </summary>
    public partial class SalesDailyModel : BaseModel
    {
        public List<SalesDaily> List { get; set; } //列表数据模型
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var list = new List<UserDto>
                    {
                        new UserDto{ UserID = 0, TrueName = " ==请选择代理== "}
                    };
                list.AddRange(UserBO.Instance.GetAgentList(OnlineTID));

                ddlAgentID.DataTextField = "TrueName";
                ddlAgentID.DataValueField = "UserID";
                ddlAgentID.DataSource = list;
                ddlAgentID.DataBind();

                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                var pageSize = int.Parse(ddlGridPageSize.SelectedValue);
                if (pageSize != Grid1.PageSize) //当每页显示记录数被改变时，页索引需要重置
                {
                    Grid1.PageSize = pageSize;
                    Grid1.PageIndex = 0;
                }

                var req = new PagingRequest
                {
                    PageIndex = Grid1.PageIndex + 1,//注意：数据表格组件分页索引从0开始
                    PageSize = Grid1.PageSize
                };

                //var condition = ddlCondition.SelectedValue;
                //var keyword = txtKeyword.Text.Trim();
                //if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(condition))
                //{
                //    req.FilterItems.Add(new FilterItem { Field = condition, Value = keyword });
                //}
                if (!Grid1.SortField.IsNullOrWhiteSpace()) req.SortExp = $"{Grid1.SortField} {Grid1.SortDirection}";

                var result = SalesDailyBO.Instance.Query(req, OnlineTID, long.Parse(ddlAgentID.SelectedValue), txtBeginDate.SelectedDate, txtEndDate.SelectedDate);
                Grid1.RecordCount = result.Total; //数据库分页初始化时，一定要设置总记录数RecordCount
                Grid1.DataSource = result.Data;
                Grid1.DataBind();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }
        }

        protected void Grid_DoAction(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Page_CustomEvent(object sender, CustomEventArgs e)
        {
            try
            {
                if (e.EventName.StartsWith("grid-")) //处理Grid控件内部事件
                {
                    var ids = e.EventArgumentsAsJObject.Value<JArray>("rowIDs").ToObject<long[]>(); //默认支持多选操作
                    switch (e.EventName)
                    {
                        case "grid-delete":
                            SalesDailyBO.Instance.Delete(ids.FirstOrDefault());
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }
            LoadData();
        }


    }
}
